## Section 3.2.1 of the pre-analysis plan: "Overall Sensitivity to Data
## Collection Procedures: Distance Between Ranks from Two Posterior
## Distributions"
##
## Kevin Quinn
## University of Michigan
##
## 8/5/2019
##

library(TopKLists)
library(coda)

set.seed(4803)

sink(file="MM3.2.1.output.txt")


########################################################################
## START a1-b1 comparisons
########################################################################

## load MCMC output
load("MM3.2.a1.M1.out.Rda")
a1.M1.out <- a1.M1.out[sample(1:nrow(a1.M1.out), nrow(a1.M1.out),
                              replace=FALSE), ]
load("MM3.2.a1.M2.out.Rda")
a1.M2.out <- a1.M2.out[sample(1:nrow(a1.M2.out), nrow(a1.M2.out),
                              replace=FALSE), ]
load("MM3.2.a1.pairwise.out.Rda")
a1.pairwise.out <- a1.pairwise.out[sample(1:nrow(a1.pairwise.out),
                                          nrow(a1.pairwise.out),
                              replace=FALSE), ]

load("MM3.2.b1.M1.out.Rda")
b1.M1.out <- b1.M1.out[sample(1:nrow(b1.M1.out), nrow(b1.M1.out),
                              replace=FALSE), ]

load("MM3.2.b1.M2.out.Rda")
b1.M2.out <- b1.M2.out[sample(1:nrow(b1.M2.out), nrow(b1.M2.out),
                              replace=FALSE), ]
load("MM3.2.b1.pairwise.out.Rda")
b1.pairwise.out <- b1.pairwise.out[sample(1:nrow(b1.pairwise.out),
                                          nrow(b1.pairwise.out),
                              replace=FALSE), ]

## make MCMC output comparable
a1.M1.out <- a1.M1.out[,1:48]
a1.M2.out <- a1.M2.out[,1:48]
b1.M1.out <- b1.M1.out[,1:48]
b1.M2.out <- b1.M2.out[,1:48]

colnames(a1.M1.out) <- gsub("as.factor\\(photoID\\)", "", colnames(a1.M1.out))
colnames(a1.M2.out) <- gsub("as.factor\\(photoID\\)", "", colnames(a1.M2.out))
colnames(b1.M1.out) <- gsub("as.factor\\(photoID\\)", "", colnames(b1.M1.out))
colnames(b1.M2.out) <- gsub("as.factor\\(photoID\\)", "", colnames(b1.M2.out))
colnames(a1.pairwise.out) <- gsub("theta\\.", "", colnames(a1.pairwise.out))
colnames(b1.pairwise.out) <- gsub("theta\\.", "", colnames(b1.pairwise.out))

for (j in 2:48){
    a1.M1.out[,j] <- a1.M1.out[,1] + a1.M1.out[,j]
    a1.M2.out[,j] <- a1.M2.out[,1] + a1.M2.out[,j]
    b1.M1.out[,j] <- b1.M1.out[,1] + b1.M1.out[,j]
    b1.M2.out[,j] <- b1.M2.out[,1] + b1.M2.out[,j]
}
colnames(a1.M1.out)[1] <- colnames(a1.pairwise.out)[1]
colnames(a1.M2.out)[1] <- colnames(a1.pairwise.out)[1]
colnames(b1.M1.out)[1] <- colnames(a1.pairwise.out)[1]
colnames(b1.M2.out)[1] <- colnames(a1.pairwise.out)[1]


M <- nrow(a1.M1.out)


## M1 vs. pairwise
spear.M1.pair <- rep(NA, M)
kend.M1.pair <- rep(NA, M)
for (iter in 1:M){
    rank.a.M <- rank(a1.M1.out[iter,])
    rank.b.M <- rank(b1.M1.out[iter,])
    rank.a.pair <- rank(a1.pairwise.out[iter,])
    rank.b.pair <- rank(b1.pairwise.out[iter,])
    d.M <- Spearman(rank.a.M, rank.b.M, 48, 48)
    d.pair <- Spearman(rank.a.pair, rank.b.pair, 48, 48)
    spear.M1.pair[iter] <- d.pair < d.M
    d.M <- Kendall2Lists(rank.a.M, rank.b.M, 48, 48, 48)
    d.pair <- Kendall2Lists(rank.a.pair, rank.b.pair, 48, 48, 48)
    kend.M1.pair[iter] <- d.pair < d.M    
}


## M2 vs. pairwise
spear.M2.pair <- rep(NA, M)
kend.M2.pair <- rep(NA, M)
for (iter in 1:M){
    rank.a.M <- rank(a1.M2.out[iter,])
    rank.b.M <- rank(b1.M2.out[iter,])
    rank.a.pair <- rank(a1.pairwise.out[iter,])
    rank.b.pair <- rank(b1.pairwise.out[iter,])
    d.M <- Spearman(rank.a.M, rank.b.M, 48, 48)
    d.pair <- Spearman(rank.a.pair, rank.b.pair, 48, 48)
    spear.M2.pair[iter] <- d.pair < d.M
    d.M <- Kendall2Lists(rank.a.M, rank.b.M, 48, 48, 48)
    d.pair <- Kendall2Lists(rank.a.pair, rank.b.pair, 48, 48, 48)
    kend.M2.pair[iter] <- d.pair < d.M    
}

cat("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
cat("a1 - b1 comparisons (coder race correlated with photos)\n")
cat("-----------------------------------------------------------\n")
cat("M1 vs. pairwise: \n")
cat("  Spearman's Footrule\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(spear.M1.pair), "\n")
cat("  Kendall's Tau\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(kend.M1.pair), "\n")
cat("-----------------------------------------------------------\n")
cat("M2 vs. pairwise: \n")
cat("  Spearman's Footrule\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(spear.M2.pair), "\n")
cat("  Kendall's Tau\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(kend.M2.pair), "\n")
cat("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n")



########################################################################
## END a1-b1 comparisons
########################################################################






########################################################################
## START a2-b2 comparisons
########################################################################

## load MCMC output
load("MM3.2.a2.M1.out.Rda")
a2.M1.out <- a2.M1.out[sample(1:nrow(a2.M1.out), nrow(a2.M1.out),
                              replace=FALSE), ]
load("MM3.2.a2.M2.out.Rda")
a2.M2.out <- a2.M2.out[sample(1:nrow(a2.M2.out), nrow(a2.M2.out),
                              replace=FALSE), ]
load("MM3.2.a2.pairwise.out.Rda")
a2.pairwise.out <- a2.pairwise.out[sample(1:nrow(a2.pairwise.out),
                                          nrow(a2.pairwise.out),
                              replace=FALSE), ]

load("MM3.2.b2.M1.out.Rda")
b2.M1.out <- b2.M1.out[sample(1:nrow(b2.M1.out), nrow(b2.M1.out),
                              replace=FALSE), ]

load("MM3.2.b2.M2.out.Rda")
b2.M2.out <- b2.M2.out[sample(1:nrow(b2.M2.out), nrow(b2.M2.out),
                              replace=FALSE), ]
load("MM3.2.b2.pairwise.out.Rda")
b2.pairwise.out <- b2.pairwise.out[sample(1:nrow(b2.pairwise.out),
                                          nrow(b2.pairwise.out),
                              replace=FALSE), ]

## make MCMC output comparable
a2.M1.out <- a2.M1.out[,1:48]
a2.M2.out <- a2.M2.out[,1:48]
b2.M1.out <- b2.M1.out[,1:48]
b2.M2.out <- b2.M2.out[,1:48]

colnames(a2.M1.out) <- gsub("as.factor\\(photoID\\)", "", colnames(a2.M1.out))
colnames(a2.M2.out) <- gsub("as.factor\\(photoID\\)", "", colnames(a2.M2.out))
colnames(b2.M1.out) <- gsub("as.factor\\(photoID\\)", "", colnames(b2.M1.out))
colnames(b2.M2.out) <- gsub("as.factor\\(photoID\\)", "", colnames(b2.M2.out))
colnames(a2.pairwise.out) <- gsub("theta\\.", "", colnames(a2.pairwise.out))
colnames(b2.pairwise.out) <- gsub("theta\\.", "", colnames(b2.pairwise.out))

for (j in 2:48){
    a2.M1.out[,j] <- a2.M1.out[,1] + a2.M1.out[,j]
    a2.M2.out[,j] <- a2.M2.out[,1] + a2.M2.out[,j]
    b2.M1.out[,j] <- b2.M1.out[,1] + b2.M1.out[,j]
    b2.M2.out[,j] <- b2.M2.out[,1] + b2.M2.out[,j]
}
colnames(a2.M1.out)[1] <- colnames(a2.pairwise.out)[1]
colnames(a2.M2.out)[1] <- colnames(a2.pairwise.out)[1]
colnames(b2.M1.out)[1] <- colnames(a2.pairwise.out)[1]
colnames(b2.M2.out)[1] <- colnames(a2.pairwise.out)[1]


M <- nrow(a2.M1.out)


## M1 vs. pairwise
spear.M1.pair <- rep(NA, M)
kend.M1.pair <- rep(NA, M)
for (iter in 1:M){
    rank.a.M <- rank(a2.M1.out[iter,])
    rank.b.M <- rank(b2.M1.out[iter,])
    rank.a.pair <- rank(a2.pairwise.out[iter,])
    rank.b.pair <- rank(b2.pairwise.out[iter,])
    d.M <- Spearman(rank.a.M, rank.b.M, 48, 48)
    d.pair <- Spearman(rank.a.pair, rank.b.pair, 48, 48)
    spear.M1.pair[iter] <- d.pair < d.M
    d.M <- Kendall2Lists(rank.a.M, rank.b.M, 48, 48, 48)
    d.pair <- Kendall2Lists(rank.a.pair, rank.b.pair, 48, 48, 48)
    kend.M1.pair[iter] <- d.pair < d.M    
}


## M2 vs. pairwise
spear.M2.pair <- rep(NA, M)
kend.M2.pair <- rep(NA, M)
for (iter in 1:M){
    rank.a.M <- rank(a2.M2.out[iter,])
    rank.b.M <- rank(b2.M2.out[iter,])
    rank.a.pair <- rank(a2.pairwise.out[iter,])
    rank.b.pair <- rank(b2.pairwise.out[iter,])
    d.M <- Spearman(rank.a.M, rank.b.M, 48, 48)
    d.pair <- Spearman(rank.a.pair, rank.b.pair, 48, 48)
    spear.M2.pair[iter] <- d.pair < d.M
    d.M <- Kendall2Lists(rank.a.M, rank.b.M, 48, 48, 48)
    d.pair <- Kendall2Lists(rank.a.pair, rank.b.pair, 48, 48, 48)
    kend.M2.pair[iter] <- d.pair < d.M    
}

cat("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
cat("a2 - b2 comparisons (distractor race correlated with photos)\n")
cat("-----------------------------------------------------------\n")
cat("M1 vs. pairwise: \n")
cat("  Spearman's Footrule\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(spear.M1.pair), "\n")
cat("  Kendall's Tau\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(kend.M1.pair), "\n")
cat("-----------------------------------------------------------\n")
cat("M2 vs. pairwise: \n")
cat("  Spearman's Footrule\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(spear.M2.pair), "\n")
cat("  Kendall's Tau\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(kend.M2.pair), "\n")
cat("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n")



########################################################################
## END a2-b2 comparisons
########################################################################








########################################################################
## START a3-b3 comparisons
########################################################################

## load MCMC output
load("MM3.2.a3.M1.out.Rda")
a3.M1.out <- a3.M1.out[sample(1:nrow(a3.M1.out), nrow(a3.M1.out),
                              replace=FALSE), ]
load("MM3.2.a3.M2.out.Rda")
a3.M2.out <- a3.M2.out[sample(1:nrow(a3.M2.out), nrow(a3.M2.out),
                              replace=FALSE), ]
load("MM3.2.a3.pairwise.out.Rda")
a3.pairwise.out <- a3.pairwise.out[sample(1:nrow(a3.pairwise.out),
                                          nrow(a3.pairwise.out),
                              replace=FALSE), ]

load("MM3.2.b3.M1.out.Rda")
b3.M1.out <- b3.M1.out[sample(1:nrow(b3.M1.out), nrow(b3.M1.out),
                              replace=FALSE), ]

load("MM3.2.b3.M2.out.Rda")
b3.M2.out <- b3.M2.out[sample(1:nrow(b3.M2.out), nrow(b3.M2.out),
                              replace=FALSE), ]
load("MM3.2.b3.pairwise.out.Rda")
b3.pairwise.out <- b3.pairwise.out[sample(1:nrow(b3.pairwise.out),
                                          nrow(b3.pairwise.out),
                              replace=FALSE), ]

## make MCMC output comparable
a3.M1.out <- a3.M1.out[,1:48]
a3.M2.out <- a3.M2.out[,1:48]
b3.M1.out <- b3.M1.out[,1:48]
b3.M2.out <- b3.M2.out[,1:48]

colnames(a3.M1.out) <- gsub("as.factor\\(photoID\\)", "", colnames(a3.M1.out))
colnames(a3.M2.out) <- gsub("as.factor\\(photoID\\)", "", colnames(a3.M2.out))
colnames(b3.M1.out) <- gsub("as.factor\\(photoID\\)", "", colnames(b3.M1.out))
colnames(b3.M2.out) <- gsub("as.factor\\(photoID\\)", "", colnames(b3.M2.out))
colnames(a3.pairwise.out) <- gsub("theta\\.", "", colnames(a3.pairwise.out))
colnames(b3.pairwise.out) <- gsub("theta\\.", "", colnames(b3.pairwise.out))

for (j in 2:48){
    a3.M1.out[,j] <- a3.M1.out[,1] + a3.M1.out[,j]
    a3.M2.out[,j] <- a3.M2.out[,1] + a3.M2.out[,j]
    b3.M1.out[,j] <- b3.M1.out[,1] + b3.M1.out[,j]
    b3.M2.out[,j] <- b3.M2.out[,1] + b3.M2.out[,j]
}
colnames(a3.M1.out)[1] <- colnames(a3.pairwise.out)[1]
colnames(a3.M2.out)[1] <- colnames(a3.pairwise.out)[1]
colnames(b3.M1.out)[1] <- colnames(a3.pairwise.out)[1]
colnames(b3.M2.out)[1] <- colnames(a3.pairwise.out)[1]


M <- nrow(a3.M1.out)


## M1 vs. pairwise
spear.M1.pair <- rep(NA, M)
kend.M1.pair <- rep(NA, M)
for (iter in 1:M){
    rank.a.M <- rank(a3.M1.out[iter,])
    rank.b.M <- rank(b3.M1.out[iter,])
    rank.a.pair <- rank(a3.pairwise.out[iter,])
    rank.b.pair <- rank(b3.pairwise.out[iter,])
    d.M <- Spearman(rank.a.M, rank.b.M, 48, 48)
    d.pair <- Spearman(rank.a.pair, rank.b.pair, 48, 48)
    spear.M1.pair[iter] <- d.pair < d.M
    d.M <- Kendall2Lists(rank.a.M, rank.b.M, 48, 48, 48)
    d.pair <- Kendall2Lists(rank.a.pair, rank.b.pair, 48, 48, 48)
    kend.M1.pair[iter] <- d.pair < d.M    
}


## M2 vs. pairwise
spear.M2.pair <- rep(NA, M)
kend.M2.pair <- rep(NA, M)
for (iter in 1:M){
    rank.a.M <- rank(a3.M2.out[iter,])
    rank.b.M <- rank(b3.M2.out[iter,])
    rank.a.pair <- rank(a3.pairwise.out[iter,])
    rank.b.pair <- rank(b3.pairwise.out[iter,])
    d.M <- Spearman(rank.a.M, rank.b.M, 48, 48)
    d.pair <- Spearman(rank.a.pair, rank.b.pair, 48, 48)
    spear.M2.pair[iter] <- d.pair < d.M
    d.M <- Kendall2Lists(rank.a.M, rank.b.M, 48, 48, 48)
    d.pair <- Kendall2Lists(rank.a.pair, rank.b.pair, 48, 48, 48)
    kend.M2.pair[iter] <- d.pair < d.M    
}

cat("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
cat("a3 - b3 comparisons (black vs. white coders)\n")
cat("-----------------------------------------------------------\n")
cat("M1 vs. pairwise: \n")
cat("  Spearman's Footrule\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(spear.M1.pair), "\n")
cat("  Kendall's Tau\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(kend.M1.pair), "\n")
cat("-----------------------------------------------------------\n")
cat("M2 vs. pairwise: \n")
cat("  Spearman's Footrule\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(spear.M2.pair), "\n")
cat("  Kendall's Tau\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(kend.M2.pair), "\n")
cat("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n")



########################################################################
## END a3-b3 comparisons
########################################################################





########################################################################
## START a4-b4 comparisons
########################################################################

## load MCMC output
load("MM3.2.a4.M1.out.Rda")
a4.M1.out <- a4.M1.out[sample(1:nrow(a4.M1.out), nrow(a4.M1.out),
                              replace=FALSE), ]
load("MM3.2.a4.M2.out.Rda")
a4.M2.out <- a4.M2.out[sample(1:nrow(a4.M2.out), nrow(a4.M2.out),
                              replace=FALSE), ]
load("MM3.2.a4.pairwise.out.Rda")
a4.pairwise.out <- a4.pairwise.out[sample(1:nrow(a4.pairwise.out),
                                          nrow(a4.pairwise.out),
                              replace=FALSE), ]

load("MM3.2.b4.M1.out.Rda")
b4.M1.out <- b4.M1.out[sample(1:nrow(b4.M1.out), nrow(b4.M1.out),
                              replace=FALSE), ]

load("MM3.2.b4.M2.out.Rda")
b4.M2.out <- b4.M2.out[sample(1:nrow(b4.M2.out), nrow(b4.M2.out),
                              replace=FALSE), ]
load("MM3.2.b4.pairwise.out.Rda")
b4.pairwise.out <- b4.pairwise.out[sample(1:nrow(b4.pairwise.out),
                                          nrow(b4.pairwise.out),
                              replace=FALSE), ]

## make MCMC output comparable
a4.M1.out <- a4.M1.out[,1:48]
a4.M2.out <- a4.M2.out[,1:48]
b4.M1.out <- b4.M1.out[,1:48]
b4.M2.out <- b4.M2.out[,1:48]

colnames(a4.M1.out) <- gsub("as.factor\\(photoID\\)", "", colnames(a4.M1.out))
colnames(a4.M2.out) <- gsub("as.factor\\(photoID\\)", "", colnames(a4.M2.out))
colnames(b4.M1.out) <- gsub("as.factor\\(photoID\\)", "", colnames(b4.M1.out))
colnames(b4.M2.out) <- gsub("as.factor\\(photoID\\)", "", colnames(b4.M2.out))
colnames(a4.pairwise.out) <- gsub("theta\\.", "", colnames(a4.pairwise.out))
colnames(b4.pairwise.out) <- gsub("theta\\.", "", colnames(b4.pairwise.out))

for (j in 2:48){
    a4.M1.out[,j] <- a4.M1.out[,1] + a4.M1.out[,j]
    a4.M2.out[,j] <- a4.M2.out[,1] + a4.M2.out[,j]
    b4.M1.out[,j] <- b4.M1.out[,1] + b4.M1.out[,j]
    b4.M2.out[,j] <- b4.M2.out[,1] + b4.M2.out[,j]
}
colnames(a4.M1.out)[1] <- colnames(a4.pairwise.out)[1]
colnames(a4.M2.out)[1] <- colnames(a4.pairwise.out)[1]
colnames(b4.M1.out)[1] <- colnames(a4.pairwise.out)[1]
colnames(b4.M2.out)[1] <- colnames(a4.pairwise.out)[1]


M <- nrow(a4.M1.out)


## M1 vs. pairwise
spear.M1.pair <- rep(NA, M)
kend.M1.pair <- rep(NA, M)
for (iter in 1:M){
    rank.a.M <- rank(a4.M1.out[iter,])
    rank.b.M <- rank(b4.M1.out[iter,])
    rank.a.pair <- rank(a4.pairwise.out[iter,])
    rank.b.pair <- rank(b4.pairwise.out[iter,])
    d.M <- Spearman(rank.a.M, rank.b.M, 48, 48)
    d.pair <- Spearman(rank.a.pair, rank.b.pair, 48, 48)
    spear.M1.pair[iter] <- d.pair < d.M
    d.M <- Kendall2Lists(rank.a.M, rank.b.M, 48, 48, 48)
    d.pair <- Kendall2Lists(rank.a.pair, rank.b.pair, 48, 48, 48)
    kend.M1.pair[iter] <- d.pair < d.M    
}


## M2 vs. pairwise
spear.M2.pair <- rep(NA, M)
kend.M2.pair <- rep(NA, M)
for (iter in 1:M){
    rank.a.M <- rank(a4.M2.out[iter,])
    rank.b.M <- rank(b4.M2.out[iter,])
    rank.a.pair <- rank(a4.pairwise.out[iter,])
    rank.b.pair <- rank(b4.pairwise.out[iter,])
    d.M <- Spearman(rank.a.M, rank.b.M, 48, 48)
    d.pair <- Spearman(rank.a.pair, rank.b.pair, 48, 48)
    spear.M2.pair[iter] <- d.pair < d.M
    d.M <- Kendall2Lists(rank.a.M, rank.b.M, 48, 48, 48)
    d.pair <- Kendall2Lists(rank.a.pair, rank.b.pair, 48, 48, 48)
    kend.M2.pair[iter] <- d.pair < d.M    
}

cat("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
cat("a4 - b4 comparisons (black vs. white distractor photos)\n")
cat("-----------------------------------------------------------\n")
cat("M1 vs. pairwise: \n")
cat("  Spearman's Footrule\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(spear.M1.pair), "\n")
cat("  Kendall's Tau\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(kend.M1.pair), "\n")
cat("-----------------------------------------------------------\n")
cat("M2 vs. pairwise: \n")
cat("  Spearman's Footrule\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(spear.M2.pair), "\n")
cat("  Kendall's Tau\n")
cat("    Pr(d(theta^a, theta^b) < d(beta^a, beta^b)) =", mean(kend.M2.pair), "\n")
cat("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n")



########################################################################
## END a4-b4 comparisons
########################################################################

sink()
